package org.summer.lawyer.handle;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.summer.lawyer.domain.vo.spuorder.SpuOrderVo;
import org.summer.lawyer.domain.vo.spuorderassign.SpuOrderAssignVo;
import org.summer.lawyer.event.SpuOrderCompletedEvent;
import org.summer.lawyer.service.IBrokerageUserService;
import org.summer.lawyer.service.IMessageService;
import org.summer.lawyer.service.ISpuOrderService;

/**
 * 订单完成处理
 */
@Slf4j
@Getter
@Component
public class SpuOrderCompletedHandle implements ApplicationListener<SpuOrderCompletedEvent> {

    @Resource
    private IBrokerageUserService brokerageUserService;
    @Resource
    private SmsService smsService;
    @Resource
    private IMessageService messageService;
    @Resource
    private ISpuOrderService spuOrderService;

    private SpuOrderAssignVo orderAssignVo;

    @Async
    @Override
    public void onApplicationEvent(SpuOrderCompletedEvent event) {
        this.orderAssignVo = event.getOrderAssignVo();
        SpuOrderVo spuOrderVo =spuOrderService.selectSpuOrderVoById(orderAssignVo.getOrderId());
        brokerageUserService.createBrokerageFee(spuOrderVo);
        //发送短信和站内消息
        if(StrUtil.isNotEmpty(orderAssignVo.getLawFirmName())) {
            messageService.completedOrderMsg(orderAssignVo);
            smsService.completedOrderSms(orderAssignVo.getContactPhone(), orderAssignVo.getUserName(), orderAssignVo.getLawFirmName());

        }

    }
}
